#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gmp.h>
#include <time.h>

int main(void){

        long double i;
        clock_t start,end;

        mpf_t aBef, b, aAft, p;
        mpf_init2(aBef,40000000);
        mpf_init2(b,40000000);
        mpf_init2(aAft,40000000);
        mpf_init2(p,40000000);

        mpf_t aux1, aux2, aux3, aux4;
        mpf_init(aux1);
        mpf_init(aux2);
        mpf_init2(aux3,40000000);
        mpf_init2(aux4,40000000);
        mpf_set_d(aux1,2);
        mpf_set_d(aux2,1);
        mpf_set_d(b,0);

        mpf_sqrt(aBef,aux1);

        mpf_add(p,aux1,aBef); //po=2+raiz2

    start = clock();
        for(i = 0;i < 12;i++){
                mpf_sqrt(aux3,aBef);

//Calcula An
                mpf_div(aux4,aux2,aux3);
                mpf_add(aux4,aux3,aux4);
                mpf_div(aAft,aux4,aux1);

//Calcula Bn
                mpf_add(aux4,aux2,b);
                mpf_mul(aux4,aux4,aux3);
                mpf_add(aux3,aBef,b);
                mpf_div(b,aux4,aux3);

//Calcula Pn
                mpf_add(aux3,aux2,aAft);
                mpf_mul(aux4,p,b);
                mpf_mul(aux4,aux3,aux4);
                mpf_add(aux3,aux2,b);
                mpf_div(p,aux4,aux3);

                mpf_set(aBef,aAft);
        }

    end=clock();
        gmp_printf("%.10000000Ff\n",p);
    printf("\n--Tempo %ld--\n",(end - start)/CLOCKS_PER_SEC);

        mpf_clear(aux1);
        mpf_clear(aux2);
        mpf_clear(aux3);
        mpf_clear(aux4);
        mpf_clear(aBef);
        mpf_clear(aAft);
        mpf_clear(b);
        mpf_clear(p);

        return 0;
}
